package views;

import views.panels.PanelGeneral;
import logicBusiness.AreaBL;
import models.Area;
import java.awt.BorderLayout;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

/**
 *
 * @author viviana
 */
public class InterfazEditarArea extends javax.swing.JFrame {

    private DefaultComboBoxModel dcb = new DefaultComboBoxModel();
    private AreaBL bo = new AreaBL();
    private Area ar = new Area();
    private int ok = 0;

    /** Creates new form InterfazEditarArea */
    public InterfazEditarArea() {
        initComponents();
        PanelGeneral panel = new PanelGeneral();
        this.add(panel, BorderLayout.SOUTH);
        CBSelecArea.setModel(dcb);
        cargarListaAreas();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        CBSelecArea = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        CTNuevoNomArea = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        CTDescripcionArea = new javax.swing.JTextField();
        BModificarArea = new javax.swing.JButton();
        BEliminarArea = new javax.swing.JButton();
        BCancelar = new javax.swing.JButton();

        setResizable(false);

        jLabel1.setText("Busque un Area Existente:");

        CBSelecArea.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                CBSelecAreaMouseClicked(evt);
            }
        });
        CBSelecArea.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CBSelecAreaActionPerformed(evt);
            }
        });

        jLabel2.setText("Nuevo Nombre:");

        jLabel3.setText("Descripción:");

        CTDescripcionArea.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CTDescripcionAreaActionPerformed(evt);
            }
        });

        BModificarArea.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        BModificarArea.setText("Modificar");
        BModificarArea.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BModificarAreaActionPerformed(evt);
            }
        });

        BEliminarArea.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        BEliminarArea.setText("Eliminar");
        BEliminarArea.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BEliminarAreaActionPerformed(evt);
            }
        });

        BCancelar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        BCancelar.setText("Cancelar");
        BCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(CTDescripcionArea, javax.swing.GroupLayout.DEFAULT_SIZE, 432, Short.MAX_VALUE)
                            .addComponent(CBSelecArea, 0, 432, Short.MAX_VALUE)
                            .addComponent(jLabel1)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(CTNuevoNomArea, javax.swing.GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE))
                            .addComponent(jLabel3)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(BModificarArea, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 54, Short.MAX_VALUE)
                                .addComponent(BEliminarArea, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(BCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(114, 114, 114))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(CBSelecArea, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(CTNuevoNomArea, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(CTDescripcionArea, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BModificarArea)
                    .addComponent(BEliminarArea))
                .addGap(18, 18, 18)
                .addComponent(BCancelar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BModificarAreaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BModificarAreaActionPerformed
        // TODO add your handling code here:
        Area tst;
        ok = 0;
        if (CTNuevoNomArea.getText().equals("") || CTDescripcionArea.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Debe digitar los datos completos para modificar un Area", null, JOptionPane.ERROR_MESSAGE);
        } else {
            tst = bo.consultarArea(CTNuevoNomArea.getText() + "");

            if (tst == null || (CBSelecArea.getSelectedItem() + "").equals(CTNuevoNomArea.getText())) {
                ok = bo.actualizarArea(CBSelecArea.getSelectedItem() + "", CTNuevoNomArea.getText(), CTDescripcionArea.getText());
                CTNuevoNomArea.setText("");
                CTDescripcionArea.setText("");//mar
            }
            if (ok == 1) {
                JOptionPane.showMessageDialog(this, "La modificación del area fue exitosa", "Modificación exitosa !!!", JOptionPane.INFORMATION_MESSAGE);
            } else {
                if (tst != null) {
                    JOptionPane.showMessageDialog(this, "Ocurrio un error al modificar el area (Nombres repetidos)", "Modificación fallida", JOptionPane.ERROR_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(this, "Ocurrio un error al modificar el area", "Modificación fallida", JOptionPane.ERROR_MESSAGE);
                }
            }
            // this.setVisible(false);t
            cargarListaAreas();
        }
        //this.setVisible(false);
    }//GEN-LAST:event_BModificarAreaActionPerformed

    private void BEliminarAreaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BEliminarAreaActionPerformed
        //        this.setVisible(false);
        if (CBSelecArea.getSelectedIndex() == -1) {
            JOptionPane.showMessageDialog(this, "No hay areas que borrar", "Eliminación fallida", JOptionPane.ERROR_MESSAGE);
        } else {
            ok = bo.encontrarArea(CBSelecArea.getSelectedItem() + "");
            if (ok != 0) {
                bo.borrarArea(ok);
                JOptionPane.showMessageDialog(this, "Eliminado correctamente", "Eliminación correcta", JOptionPane.OK_OPTION);
                cargarListaAreas();
            }
        }







    }//GEN-LAST:event_BEliminarAreaActionPerformed

    private void CBSelecAreaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CBSelecAreaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_CBSelecAreaActionPerformed

    private void CBSelecAreaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_CBSelecAreaMouseClicked
        /*     int indiceLista = comboAreas.getSelectedIndex();
        if (indiceLista != -1) {
        //ar = bo.instanciarArea(comboAreas.getSelectedObjects());
        ar = (Area) comboAreas.getSelectedItem();
        entrada1.setText(ar.getNombreArea());
        entrada2.setText(ar.getDescripcionArea());
        //entrada1.setText("nada");
        // entrada2.setText("xP");
        }*/
    }//GEN-LAST:event_CBSelecAreaMouseClicked

    private void CTDescripcionAreaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CTDescripcionAreaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_CTDescripcionAreaActionPerformed

    private void BCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BCancelarActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_BCancelarActionPerformed

    /**
     * Este metodo permite que los nombres de las areas que se encuentran
     * en la BD aparezcan en el comboBox (comboAreas)
     */
    private void cargarListaAreas() {
        List l = bo.consultarAreas();
        DefaultComboBoxModel modelo = (DefaultComboBoxModel) CBSelecArea.getModel();
        modelo.removeAllElements();
        for (int i = 0; i < l.size(); i++) {
            modelo.addElement(l.get(i));

        }
        CBSelecArea.setModel(modelo);

    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new InterfazEditarArea().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BCancelar;
    private javax.swing.JButton BEliminarArea;
    private javax.swing.JButton BModificarArea;
    private javax.swing.JComboBox CBSelecArea;
    private javax.swing.JTextField CTDescripcionArea;
    private javax.swing.JTextField CTNuevoNomArea;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    // End of variables declaration//GEN-END:variables
}
